//
//  UIImage+Scale.swift
//  broser
//
//  Created by 李伟鹏 on 16/5/25.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

extension UIImage{
    
    func scaleImage(newWidth: CGFloat) -> UIImage{
        if self.size.width <= newWidth{
            return self
        }
        
        let newHeight = newWidth * self.size.height / self.size.width
        
        let newSize = CGSize(width: newWidth, height: newHeight)
        
        UIGraphicsBeginImageContext(newSize)
        
        self.drawInRect(CGRect(origin: CGPointZero, size: newSize))
        
        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        
        UIGraphicsEndImageContext()
        return newImage
    }
    
}
